package com.example;

public class SharedResource {
    private int sharedValue = 0;

    // 同步方法：增加 sharedValue
    public synchronized void increment() {
        sharedValue++;
    }

    // 同步方法：打印 sharedValue
    public synchronized void printValue() {
        System.out.println("Current value: " + sharedValue);
    }

    public static void main(String[] args) {
        SharedResource resource = new SharedResource();

        // 线程1：调用 increment() 10 次
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resource.increment();
            }
        });

        // 线程2：调用 printValue() 10 次
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                resource.printValue();
            }
        });

        thread1.start();
        thread2.start();
    }
}